#!/bin/bash

function help() {
  echo "Usage: './aaf.sh start|stop|restart all' or './aaf.sh start|stop|restart|status <service> [<service>]'"
  echo "      where <service> is one of the following: xrootd, proof, afdsmgrd, aaf-mlclient, sync-alien-packages-cron, restart-proof-if-down ."
  echo "      'all' is equivalent to 'proof xrootd'."

}

function checkAll() {
#  echo "User is `whoami`"

  if [ "`whoami`" == "root" ]; then
    echo "User is `whoami` and it is OK ..."
  else
    echo "You have to run this script as super user !!!!"
    exit 1
  fi
  if [ "${0:0:1}" == "/" ]; then
    MY_DIR=`dirname $0`
    MY_AAF_DIR=`dirname $MY_DIR`
  else
    MY_DIR=`pwd`
    MY_AAF_DIR=`dirname $MY_DIR`
  fi

  if [ ! -e $MY_AAF_DIR/aaf.cf ]; then
    echo "$MY_AAF_DIR/aaf.cf was not found !!!!!!"
    exit 2
  fi

  source $MY_AAF_DIR/aaf.cf
}

function do_upgrade_xrootd() {
  if [ `ls -la $ALICE_PROOF_AAF_DIR | grep xrootd_ | wc -l` -gt 1 ]; then
    echo "Doing upgrade ..."
    echo "Stopping old xrootd ..."
    for MY_XRD_VER in `ls -rt $ALICE_PROOF_AAF_DIR | grep xrootd_` ; do
#      echo $MY_XRD_VER
      if [ `ps aux | grep "$MY_XRD_VER/bin/xrootd" | grep -v grep | wc -l` -gt 0 ]; then
#	echo "$MY_XRD_VER is running ..."
	if [ "$MY_XRD_VER" == "`ls $ALICE_PROOF_AAF_DIR | grep xrootd_ | tail -n 1`" ]; then
	  echo "$MY_XRD_VER is running BUT It is latest verion..."
	else
	  echo "$MY_XRD_VER is running so we are stoping it ..."
	  $ALICE_PROOF_AAF_DIR/$MY_XRD_VER/scripts/xrootd/xrd.sh -k
	fi
      else
        echo "$MY_XRD_VER is NOT running ..."
      fi
    done

    pkill -u $ALICE_PROOF_AAF_USER perl

    for MY_XRD_VER in `ls -rt $ALICE_PROOF_AAF_DIR | grep xrootd_` ; do
      echo "Cleaning $MY_XRD_VER ..."
      if [ "$MY_XRD_VER" == "`ls -rt $ALICE_PROOF_AAF_DIR | grep xrootd_ | tail -n 1`" ]; then
          echo "$MY_XRD_VER is NOT removed because it is latest verion..."
      else
          echo "$MY_XRD_VER is removed ..."
          rm -Rf $ALICE_PROOF_AAF_DIR/$MY_XRD_VER/
      fi
    done

  fi
}
function do_upgrade_aaf() {
  ISML=`cat $ALICE_PROOF_AAF_DIR/aaf.cf | grep ALICE_PROOF_AAF_ML_REPOSITORY`
  if [ "x$ISML" == "x" ]; then
    echo "" >> $ALICE_PROOF_AAF_DIR/aaf.cf
    echo "# define monalisa reopsitary url here" >> $ALICE_PROOF_AAF_DIR/aaf.cf
    echo 'export ALICE_PROOF_AAF_ML_REPOSITORY=""' >> $ALICE_PROOF_AAF_DIR/aaf.cf
  fi
}
function do_upgrade_proof() {
# echo "In function do_upgrade_proof"
  rm -f /etc/init.d/xproofd
  ln -s  $ALICE_PROOF_AAF_DIR/proof/aaf-proof/scripts/xproofd.rc.d /etc/init.d/xproofd
  rm -f /etc/sysconfig/xproofd
  ln -s  $ALICE_PROOF_AAF_DIR/proof/aaf-proof/scripts/xproofd.sysconfig /etc/sysconfig/xproofd
}

function do_upgrade_afdsmgrd() {
echo "In function do_upgrade_afdsmgrd"
}

function do_upgrade_aaf-mlclient() {
# echo "In function do_upgrade_monalisa"

  rm -f /etc/init.d/aaf-mlclient
  ln -s  $ALICE_PROOF_AAF_DIR/scripts/aaf-mlclient/aaf-mlclient.init.d /etc/init.d/aaf-mlclient
  rm -f /etc/sysconfig/aaf-mlclient
  ln -s  $ALICE_PROOF_AAF_DIR/scripts/aaf-mlclient/aaf-mlclient.sysconfig /etc/sysconfig/aaf-mlclient

}

function do_proof() {
  /etc/init.d/xproofd $1
}

function do_xrootd() {
  MY_XRD_VER=`ls $ALICE_PROOF_AAF_DIR | grep xrootd_ | tail -n 1`
  if [ "$1" == "start" ];then
    echo "xrootd -f"
    su - $ALICE_PROOF_AAF_USER -c "$ALICE_PROOF_AAF_DIR/$MY_XRD_VER/scripts/xrootd/xrd.sh -f"
  fi

  if [ "$1" == "restart" ];then
    su - $ALICE_PROOF_AAF_USER -c "$ALICE_PROOF_AAF_DIR/$MY_XRD_VER/scripts/xrootd/xrd.sh -f"
  fi

  if [ "$1" == "stop" ];then
    su - $ALICE_PROOF_AAF_USER -c "$ALICE_PROOF_AAF_DIR/$MY_XRD_VER/scripts/xrootd/xrd.sh -k"
  fi

  if [ "$1" == "status" ];then
    su - $ALICE_PROOF_AAF_USER -c "$ALICE_PROOF_AAF_DIR/$MY_XRD_VER/scripts/xrootd/xrd.sh"
  fi

}

function do_aaf-mlclient() {
# echo "In function do_monalisa"
/etc/init.d/aaf-mlclient $1
}

function do_afdsmgrd() {
  /etc/init.d/afdsmgrd $1
}

function do_sync-alien-packages-cron() {
# Parameters to this function can be "start", "stop" and "status". Anything else is ignored.
# "start": Adds command to the crontab of $ALICE_PROOF_AAF_USER to do "aaf-install --sync-alien-packages" twice per day: at 5:00 and 20:00.
# "stop": Removes command associated with "aaf-install --sync-alien-packages" from the crontab of $ALICE_PROOF_AAF_USER.
# "status": Displays the "aaf-install --sync-alien-packages" command in the crontab of $ALICE_PROOF_AAF_USER if present. Otherwise displays
# message indicating that the command is not present in the crontab. 

  local tmpfile_start="/tmp/sync_alien_packages_cron_start".`date +%d.%m.%Y_%H.%M.%S`
  local tmpfile_stop="/tmp/sync_alien_packages_cron_stop".`date +%d.%m.%Y_%H.%M.%S`
  case $1 in
	start)
	  crontab -l -u $ALICE_PROOF_AAF_USER 1> $tmpfile_start 2> /dev/null 
	  is_present=`grep -c "aaf-installer --sync-alien-packages-cron" $tmpfile_start`

	  if [ $is_present = 0 ]; then
	    echo "Adding sync-alien-packages-cron to crontab ..."
	    if [ ! -d $ALICE_PROOF_AAF_DIR/logs/sync_alien_cron/ ]; then
	      mkdir -p $ALICE_PROOF_AAF_DIR/logs/sync_alien_cron/
	      chown $ALICE_PROOF_AAF_USER:$ALICE_PROOF_AAF_GROUP $ALICE_PROOF_AAF_DIR/logs/sync_alien_cron/
	    fi
	    local LOGFILE="$ALICE_PROOF_AAF_DIR/logs/sync_alien_cron/sync_alien_packages_cron.log"
	    echo "0 5,20 * * * export AAF_CF_DIR=$ALICE_PROOF_AAF_DIR/ && $ALICE_PROOF_AAF_DIR/scripts/aaf-installer.sh --sync-alien-packages-cron > $LOGFILE 2>&1" >> $tmpfile_start
	  if [ -e $tmpfile_start ]; then
	    crontab -u $ALICE_PROOF_AAF_USER $tmpfile_start
	    rm -f $tmpfile_start
	  fi
	  else
	    echo "The crontab of user $ALICE_PROOF_AAF_USER already contains sync-alien-packages-cron command. Use \"status\" option to list it. You can use \"stop\" option to delete the entry in the crontab. Exitting..."
	    exit
	  fi
	;;

	stop)
	  echo "Removing sync-alien-packages-cron from crontab ..."
# 	  crontab -l -u $ALICE_PROOF_AAF_USER | grep -Ev '^.*aaf-installer --sync-alien-packages-cron.*$' 1>$tmpfile_stop 2> /dev/null
	  crontab -l -u $ALICE_PROOF_AAF_USER | grep -v 'sync-alien-packages-cron' 1>$tmpfile_stop 2> /dev/null
	  if [ -e $tmpfile_stop ]; then
	    crontab -u $ALICE_PROOF_AAF_USER $tmpfile_stop
	    rm -f $tmpfile_stop
	  fi
	;;

	restart)
	  do_sync-alien-packages-cron stop
	  do_sync-alien-packages-cron start
	;;

	status)
	  echo "crontab for user $ALICE_PROOF_AAF_USER contains:"
	  crontab -l -u $ALICE_PROOF_AAF_USER
	;;

	*)
	  echo "The following commands are supported: start, stop, status. Command $1 is ignored."
	;;
  esac
}

function do_restart-proof-if-down() {

  local tmpfile_start="/tmp/restart_proof_if_down_start".`date +%d.%m.%Y_%H.%M.%S`
  local tmpfile_stop="/tmp/restart_proof_if_down_stop".`date +%d.%m.%Y_%H.%M.%S`
  case $1 in
  start)
    crontab -l 1> $tmpfile_start 2> /dev/null 
    is_present=`grep -c "restart_if_down" $tmpfile_start`

    if [ $is_present = 0 ]; then
      echo "Adding restart_proof_if_down to crontab ..."
      echo "*/5 * * * * /etc/init.d/xproofd restart_if_down &> /dev/null" >> $tmpfile_start
    if [ -e $tmpfile_start ]; then
      crontab $tmpfile_start
      rm -f $tmpfile_start
    fi
    else
      echo "The crontab  already contains restart of proof command. Use \"status\" option to list it. You can use \"stop\" option to delete the entry in the crontab. Exitting..."
      exit
    fi
  ;;

  stop)
    echo "Removing restart_proof_if_down from crontab ..."
#     crontab -l -u $ALICE_PROOF_AAF_USER | grep -Ev '^.*aaf-installer --sync-alien-packages-cron.*$' 1>$tmpfile_stop 2> /dev/null
    crontab -l | grep -v 'restart_if_down' 1>$tmpfile_stop 2> /dev/null
    if [ -e $tmpfile_stop ]; then
      crontab $tmpfile_stop
      rm -f $tmpfile_stop
    fi
  ;;

  restart)
    do_restart-proof-if-down stop
    do_restart-proof-if-down start
  ;;

  status)
    echo "crontab for user root contains:"
    crontab -l 
  ;;

  *)
    echo "The following commands are supported: start, stop, status. Command $1 is ignored."
  ;;
  esac
}


if [ $# -lt 2 ]; then
  help
  exit 1
fi

cmd=$1
shift

if [ "$cmd" != "start" -a "$cmd" != "stop" -a "$cmd" != "restart" -a "$cmd" != "status" ]; then
  echo "$cmd: No such command"
  help
  exit 1
fi

while [[ $* ]]; do
    service=$1; 
    shift  # getting service name 

    case $service in    
	all)
	  checkAll
	  do_upgrade_aaf
	  do_upgrade_xrootd
	  do_upgrade_aaf-mlclient
          do_proof "$cmd"
   	  do_aaf-mlclient "$cmd"   
	  do_xrootd "$cmd"
	  do_sync-alien-packages-cron "$cmd" 
    do_restart-proof-if-down "$cmd" 

        # do_afdsmgrd "$cmd"
	  ;;

	proof|xrootd|afdsmgrd|aaf-mlclient)
	  checkAll
	  do_upgrade_aaf
	  do_upgrade_$service
	  do_$service "$cmd"
	  ;;

  sync-alien-packages-cron)
    checkAll
    do_$service "$cmd"    
    ;;  
  restart-proof-if-down)
    checkAll
    do_$service "$cmd"    
    ;;  
	*)
	  echo "$service: No such service"
	  help
	  exit 1 
	  ;;
    esac
done
